local Class = require('lua.lang.Class')
local OutputStream = require('lua.io.OutputStream')

--- @class DataOutputStream : OutputStream
--- @field out OutputStream
local DataOutputStream = Class {
	__extends = OutputStream
}

--- @param out OutputStream
--- @return DataOutputStream
function DataOutputStream:new(out)
	--- @type DataOutputStream
	local o = {}
	o.out = out
	return setmetatable(o, DataOutputStream)
end

--- @param i number
--- @return void
function DataOutputStream:write(i)
	self.out:write(i)
end

--- @param b number
--- @return void
function DataOutputStream:writeByte(b)
	self:write(b % 256)
end

--- @param i number
--- @return void
function DataOutputStream:writeInt(i)
	for shift = 24, 0, -8 do
		self:write((i >> shift) & 0xFF)
	end
end

--- @param l number
--- @return void
function DataOutputStream:writeLong(l)
	for i = 7, 0, -1 do
		self:write((l >> (i * 8)) & 0xFF)
	end
end

--- @return void
function DataOutputStream:close()
	self.out:close()
	self.out = nil
end

return DataOutputStream